package com.shoujifeng.snowmusic.player;

import com.shoujifeng.snowmusic.entity.AnimaAccess;
import com.shoujifeng.snowmusic.player.R;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TabHost;

public class NativeMusicTab extends TabActivity {
	private Button musicBtn;
	private Button playerBtn;
	private Button specialBtn;
	private Intent mIntent1;
	private Intent mIntent2;
	private Intent mIntent3;
	private final int TAB_1 = 0;
	private final int TAB_2 = 1;
	private final int TAB_3 = 2;
	private int LAST_TAB = 0;
	private TabHost mTabHost;
	private View mTmpView;
	private ImageButton breakBtn;
	
	private AnimaAccess mAnimaAccess;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.native_music_tab);
		init();

		mAnimaAccess = new AnimaAccess(this);
	}
	
	@Override
	protected void onPause() {
		
		super.onPause();
	}

	private void init() {
		mTabHost = getTabHost();
		musicBtn = (Button) findViewById(R.id.firstBtn);
		playerBtn = (Button) findViewById(R.id.secondBtn);
		specialBtn = (Button) findViewById(R.id.thirdBtn);
		breakBtn = (ImageButton) findViewById(R.id.breakBtn);
		musicBtn.setOnClickListener(onClickListener);
		playerBtn.setOnClickListener(onClickListener);
		specialBtn.setOnClickListener(onClickListener);
		breakBtn.setOnClickListener(onClickListener);
		mTmpView = musicBtn;

		mIntent1 = new Intent(this, NativeMusicActivity.class);
		mIntent2 = new Intent(this, NativePlayerActivity.class);
		mIntent3 = new Intent(this,NativeSpecialActivity.class);
		mIntent1.putExtra("mIntent1", true);
		mIntent2.putExtra("mIntent2", true);
		mIntent3.putExtra("mIntent3", true);
		setTabIndicator("", 1, mIntent1);
		setTabIndicator("", 2, mIntent2);
		setTabIndicator("", 3, mIntent3);
		
		musicBtn.setTextColor(getResources().getColor(R.color.white));
	}

	private View.OnClickListener onClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			if (mTmpView == v)
				return;

			int animTime = 300;
			
			switch (v.getId()) {

			case R.id.firstBtn: {
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, 1.0f, 0f, 0f, animTime, false));
				mTabHost.setCurrentTab(TAB_1);
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(-1.0f, 0f, 0f, 0f, animTime, false));
				
				musicBtn.setBackgroundResource(R.drawable.online_menu_bg);
				playerBtn.setBackgroundResource(R.drawable.btn_bg_1);
				specialBtn.setBackgroundResource(R.drawable.btn_bg_1);
				
				musicBtn.setTextColor(getResources().getColor(R.color.white));
				playerBtn.setTextColor(getResources().getColor(R.color.black));
				specialBtn.setTextColor(getResources().getColor(R.color.black));
				
				LAST_TAB = TAB_1;
			}
				break;
			case R.id.secondBtn: {
				if(LAST_TAB > TAB_2) {
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, 1.0f, 0f, 0f, animTime, false));
					mTabHost.setCurrentTab(TAB_2);
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(-1.0f, 0f, 0f, 0f, animTime, false));
					
				}
				else {
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, -1.0f, 0f, 0f, animTime, false));
					mTabHost.setCurrentTab(TAB_2);
					mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(1.0f, 0f, 0f, 0f, animTime, false));
					
				}
				
				playerBtn.setBackgroundResource(R.drawable.online_menu_bg);
				musicBtn.setBackgroundResource(R.drawable.btn_bg_1);
				specialBtn.setBackgroundResource(R.drawable.btn_bg_1);
				
				musicBtn.setTextColor(getResources().getColor(R.color.black));
				playerBtn.setTextColor(getResources().getColor(R.color.white));
				specialBtn.setTextColor(getResources().getColor(R.color.black));
				
				LAST_TAB = TAB_2;
			}
				break;
			case R.id.thirdBtn: {
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(0f, -1.0f, 0f, 0f, animTime, false));
				mTabHost.setCurrentTab(TAB_3);
				mTabHost.getCurrentView().startAnimation(mAnimaAccess.moveAnimationSelf(1.0f, 0f, 0f, 0f, animTime, false));
				
				specialBtn.setBackgroundResource(R.drawable.online_menu_bg);
				musicBtn.setBackgroundResource(R.drawable.btn_bg_1);
				playerBtn.setBackgroundResource(R.drawable.btn_bg_1);
				
				musicBtn.setTextColor(getResources().getColor(R.color.black));
				playerBtn.setTextColor(getResources().getColor(R.color.black));
				specialBtn.setTextColor(getResources().getColor(R.color.white));
				
				LAST_TAB = TAB_3;
			}
				break;
			case R.id.breakBtn:{
				finish();
				//((MyTabHost)getParent()).returnToActivity(0);
				overridePendingTransition(R.anim.no_move, R.anim.right_out);
			}	

			default:
				break;
			}

			mTmpView = v;
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
		{
			finish();
			
			overridePendingTransition(R.anim.no_move, R.anim.right_out);
		}
			break;

		default:
			break;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	
	
	private void setTabIndicator(String titleString, int nId, Intent intent) {
		// 使用指定样式的TAB
		View view = LayoutInflater.from(this.mTabHost.getContext()).inflate(
				R.layout.tab_style, null);
		String idString = String.valueOf(nId);
		// 创建一个新Tab
		TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(idString)
				.setIndicator(view).setContent(intent);
		mTabHost.addTab(localTabSpec);
	}
}
